﻿using System;
using Kaleida.ServiceMonitor.Operations.ResponseHandlers;
using NUnit.Framework;

namespace Kaleida.UnitTests.Operations.ResponseHandlers
{
    [TestFixture]
    public class MustNotContainTests
    {
        [Test]
        public void TestStringArgument()
        {
            Assert.AreEqual("String was not found within document of length 1 character", new MustNotContain("1").ProcessResponse("2"));
            Assert.AreEqual("String was not found within document of length 3 characters", new MustNotContain("123").ProcessResponse("456"));
            Assert.AreEqual("String was not found within document of length 8 characters", new MustNotContain("-123").ProcessResponse(" -456789"));
            Assert.AreEqual("String was not found within document of length 20 characters", new MustNotContain(@"line1\r\nline2").ProcessResponse(" linea\r\nlineb\r\nline3"));

            var exception1 = Assert.Throws<InvalidOperationException>(() => new MustNotContain("").ProcessResponse("abcd"));
            Assert.AreEqual("Expected response to not contain '' but text was found", exception1.Message);
            
            var exception2 = Assert.Throws<InvalidOperationException>(() => new MustNotContain("123").ProcessResponse("01234"));
            Assert.AreEqual("Expected response to not contain '123' but text was found", exception2.Message);

            var exception3 = Assert.Throws<InvalidOperationException>(() => new MustNotContain("123").ProcessResponse("123"));
            Assert.AreEqual("Expected response to not contain '123' but text was found", exception3.Message);
        }

        [Test]
        public void TestStringContainerArgument()
        {
            Assert.AreEqual("String was not found within document of length 1 character", new MustNotContain("1").ProcessResponse(new MockStringContainer("2")), "Content is used in preference to ToString()");
            Assert.AreEqual("String was not found within document of length 3 characters", new MustNotContain("123").ProcessResponse(new MockStringContainer("456")), "Content is used in preference to ToString()");
            Assert.AreEqual("String was not found within document of length 8 characters", new MustNotContain("-123").ProcessResponse(new MockStringContainer(" -456789")), "Content is used in preference to ToString()");
            Assert.AreEqual("String was not found within document of length 20 characters", new MustNotContain(@"line1\r\nline2").ProcessResponse(new MockStringContainer(" linea\r\nlineb\r\nlinec")));

            var exception1 = Assert.Throws<InvalidOperationException>(() => new MustNotContain("").ProcessResponse(new MockStringContainer("abcd")));
            Assert.AreEqual("Expected response to not contain '' but text was found", exception1.Message);

            var exception2 = Assert.Throws<InvalidOperationException>(() => new MustNotContain("123").ProcessResponse(new MockStringContainer("01234")));
            Assert.AreEqual("Expected response to not contain '123' but text was found", exception2.Message);

            var exception3 = Assert.Throws<InvalidOperationException>(() => new MustNotContain("123").ProcessResponse(new MockStringContainer("123")));
            Assert.AreEqual("Expected response to not contain '123' but text was found", exception3.Message);
        }
    }
}
